﻿/*
 * TextWriter.h
 */

#ifndef __GDK_TEXTWRITER_H__
#define __GDK_TEXTWRITER_H__


#include <gdk/core/Types.h>
#include <gdk/core/Stream.h>



namespace gdk
{



	class GDK_CORE_EXPORT TextWriter
	{
		class IWriter : public Unknown
		{
			DECLARE_CLASS_HANDLE(IWriter)

		public:
			virtual bool write(const StringAnsi &) =0;
			virtual bool write(const StringWide &) =0;
			virtual bool write(const StringUtf8 &) =0;
		};

		class AnsiWriter : public TextWriter::IWriter
		{
			DECLARE_CLASS_HANDLE(AnsiWriter)
			DECLARE_CLASS_INSTANCE1(AnsiWriter, Output*);

		private:
			Output::Handle _o;

		public:
			__INLINE__ AnsiWriter(Output *o)
				: _o(o)
			{
			}

			virtual bool write(const StringAnsi &);
			virtual bool write(const StringWide &);
			virtual bool write(const StringUtf8 &);
		};

		class WideWriter : public TextWriter::IWriter
		{
			DECLARE_CLASS_HANDLE(WideWriter)
			DECLARE_CLASS_INSTANCE1(WideWriter, Output*);

		private:
			Output::Handle _o;

		public:
			__INLINE__ WideWriter(Output *o)
				: _o(o)
			{
			}

			virtual bool write(const StringAnsi &);
			virtual bool write(const StringWide &);
			virtual bool write(const StringUtf8 &);
		};

		class Utf8Writer : public TextWriter::IWriter
		{
			DECLARE_CLASS_HANDLE(Utf8Writer)
			DECLARE_CLASS_INSTANCE1(Utf8Writer, Output*);

		private:
			Output::Handle _o;

		public:
			__INLINE__ Utf8Writer(Output *o)
				: _o(o)
			{
			}

			virtual bool write(const StringAnsi &);
			virtual bool write(const StringWide &);
			virtual bool write(const StringUtf8 &);
		};

	public:
		enum CharacterSet
		{
			enumUtf8,
			enumWide,
			enumAnsi,
		};

	private:
		IWriter::Handle	_writer;

	public:
		__INLINE__ TextWriter(void)
		{
		}
		__INLINE__ TextWriter(Output *o, CharacterSet characterSet = enumUtf8)
		{
			open(o, characterSet);
		}

		bool open(Output *o, CharacterSet characterSet = enumUtf8);
		__INLINE__ void close(void)
		{
			_writer = NULL;
		}

#ifdef TEXTWRITER_WRITE
#undef TEXTWRITER_WRITE
#endif
#define TEXTWRITER_WRITE(String) \
		__INLINE__ bool write(const String &text)\
		{\
			return (_writer != NULL) ? _writer->write(text) : false;\
		}


		TEXTWRITER_WRITE(StringAnsi);
		TEXTWRITER_WRITE(StringWide);
		TEXTWRITER_WRITE(StringUtf8);

		ssize_t vprintf(const Char *fmt, va_list args);
		ssize_t print(const Char *fmt, ...) __PRINTF_FORMAT(2, 3);
	};




}//namespace gdk


#endif //__GDK_TEXTWRITER_H__
